﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Windows.Input;
using System.Globalization;
using System.Threading;
using static System.Net.WebRequestMethods;

namespace OneSys.Misc
{
    partial class AboutBox : DevExpress.XtraEditors.XtraForm
    {
  

        [System.Runtime.InteropServices.DllImport("DzSddzSoft.dll")]	
        static extern int GetRemaingDays();

        private static AboutBox theOne = null;
        private static void CallBack(
            int code, IntPtr param, IntPtr lParam)
        {

            if (theOne != null && code >= 0 && param == (IntPtr)InterceptKeys.WM_KEYDOWN)
            {

              

                var vkCode = KeyInterop.KeyFromVirtualKey(Marshal.ReadInt32(lParam));
                if (vkCode == Key.V && InterceptKeys.IsKeyPressed(
                                        KeyInterop.VirtualKeyFromKey(Key.LeftCtrl))
                                    && InterceptKeys.IsKeyPressed(KeyInterop.VirtualKeyFromKey(Key.LeftAlt)))
                {


                    DateTime c3 = System.IO.File.GetLastWriteTime(Assembly.GetEntryAssembly().Location);

                    theOne.Versionlabel.Text = String.Format("{0} {1}({2})", theOne.resources.GetString("Versionlabel.Text"),
                        theOne.AssemblyVersion, c3.ToString("yy.MM.dd HH:mm"));

                    theOne.layoutControlItem4.ContentVisible = true;
                }


            }
        }

        public AboutBox():this(null, null)
        {
           
        }

 
        public AboutBox(Image mainImage, Image minorImage)
        {
            
            InitializeComponent();

        
            this.Text = String.Format("{0} {1}", resources.GetString("$this.Text"), AssemblyTitle);
            
            this.Versionlabel.Text = String.Format("{0} {1}", resources.GetString("Versionlabel" +
                "" +
                "" +
                "" +
                ".Text"), string.Join(".", AssemblyVersion.Split(".".ToCharArray()).Take(2)));

            this.CopyRightlabel.Text = AssemblyCopyright;
            this.LicLabel.Text = AssemblyLicRemainDays;
            this.ProductRight.Text = AssemblyProduct;
            if(mainImage != null)
            {
                this.pictureBoxMain.Image = mainImage;
            }
            if(minorImage != null)
            {
                this.pictureBoxMinor.Image = minorImage;
            }

            InterceptKeys.SetHook(CallBack);
            theOne = this;
            this.layoutControlItem4.ContentVisible = false;
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                
                return Assembly.GetEntryAssembly().GetName().Version.ToString();
            }
        }


        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                string CopyRight = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
                CopyRight = string.Format("{0} {1}", CopyRight, resources.GetString("CopyRightlabel.Text"));
                return CopyRight;
            }
        }
        public string AssemblyLicRemainDays
        {
            get
            {
                string LicRemainDaysInfo = resources.GetString("LicRemainDaysLabel.Text");
                LicRemainDaysInfo = LicRemainDaysInfo.Replace("number", GetRemaingDays().ToString());              
                return LicRemainDaysInfo;
            }
        }
        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        public Icon AppIcon
        {
            get
            {
               return Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
            }
        }
        #endregion

        private void okButton_Click(object sender, EventArgs e)
        {

        }


      


        private void Aboutbox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {

            if (e.Control && e.Alt && e.KeyCode == Keys.V)
            {
                this.Versionlabel.Text = String.Format("{0} {1}", resources.GetString("Versionlabel.Text"), AssemblyVersion);              
            }
        }

    }
}
